//
//  HistoryGroupProvider.m
//  ZingBrowser
//
//  Created by -Xh- on 3/6/13.
//  Copyright (c) 2013 -Xh-. All rights reserved.
//

#import "HistoryGroupProvider.h"

@implementation HistoryGroupProvider
@synthesize groupDataListArray;
@synthesize groupDisplayNameArray;




-(id)init{
    self=[super init];
    if(self){
        groupDisplayNameArray=[[NSMutableArray alloc]init];
        [groupDisplayNameArray addObject:[MyUtilities getLocalizeStringFromTag:@"HistoryToday"]];
        [groupDisplayNameArray addObject:[MyUtilities getLocalizeStringFromTag:@"HistoryYesterday"]];
        [groupDisplayNameArray addObject:[MyUtilities getLocalizeStringFromTag:@"HistoryLast7Days"]];
        [groupDisplayNameArray addObject:[MyUtilities getLocalizeStringFromTag:@"HistoryLast30Days"]];
        
        groupDataListArray=[[NSMutableArray alloc]init];
        
    }
    return self;
}
-(void)relocalize{
    [groupDisplayNameArray removeAllObjects];
    [groupDisplayNameArray addObject:[MyUtilities getLocalizeStringFromTag:@"HistoryToday"]];
    [groupDisplayNameArray addObject:[MyUtilities getLocalizeStringFromTag:@"HistoryYesterday"]];
    [groupDisplayNameArray addObject:[MyUtilities getLocalizeStringFromTag:@"HistoryLast7Days"]];
    [groupDisplayNameArray addObject:[MyUtilities getLocalizeStringFromTag:@"HistoryLast30Days"]];

    [self updateSectionTitle];

    
}

-(void)updateSectionTitle{
    for(int i=0;i<groupDisplayNameArray.count;i++){
        NSMutableArray* dataListOnEachGroup=[groupDataListArray objectAtIndex:i];
        NSString* iDisplayName=[groupDisplayNameArray objectAtIndex:i];
        switch (i) {
            case Today:
                iDisplayName=[[MyUtilities getLocalizeStringFromTag:@"HistoryToday"] stringByAppendingFormat:@" (%i)",dataListOnEachGroup.count];
                break;
                
            case Yesterday:
                iDisplayName=[[MyUtilities getLocalizeStringFromTag:@"HistoryYesterday"] stringByAppendingFormat:@" (%i)",dataListOnEachGroup.count];
                break;
            case Last7Days:
                iDisplayName=[[MyUtilities getLocalizeStringFromTag:@"HistoryLast7Days"] stringByAppendingFormat:@" (%i)",dataListOnEachGroup.count];
                break;
            case Last30Days:
                iDisplayName=[[MyUtilities getLocalizeStringFromTag:@"HistoryLast30Days"] stringByAppendingFormat:@" (%i)",dataListOnEachGroup.count];
                break;
        }
        [groupDisplayNameArray replaceObjectAtIndex:i withObject:iDisplayName];
    }
}

-(void)update{
    double numberOfSecondsInOneDay=60*60*24;
    [groupDataListArray removeAllObjects];
    for(int i=0;i<groupDisplayNameArray.count;i++){
        NSMutableArray* dataListOnEachGroup=[[NSMutableArray alloc]init];
        NSString* iDisplayName=[groupDisplayNameArray objectAtIndex:i];
        switch (i) {
            case Today:
                dataListOnEachGroup= [[[StaticVariables shareVariables]historyAccess] getAllRecordsOnSpecificDate:[NSDate date]];
                iDisplayName=[[MyUtilities getLocalizeStringFromTag:@"HistoryToday"] stringByAppendingFormat:@" (%i)",dataListOnEachGroup.count];
                break;
                
            case Yesterday:
                dataListOnEachGroup= [[[StaticVariables shareVariables]historyAccess] getAllRecordsOnSpecificDate:[NSDate dateWithTimeIntervalSinceNow:-numberOfSecondsInOneDay]];
                iDisplayName=[[MyUtilities getLocalizeStringFromTag:@"HistoryYesterday"] stringByAppendingFormat:@" (%i)",dataListOnEachGroup.count];
                break;
                
            case Last7Days:
                dataListOnEachGroup=[[[StaticVariables shareVariables]historyAccess] getAllRecordsBetweenDate:[NSDate dateWithTimeIntervalSinceNow:-7*numberOfSecondsInOneDay]
                                                                    andDate:[NSDate date]];
                //exclude all objects belong to previous list
                for(int j=0;j<i;j++){
                    NSMutableArray* previousList=[groupDataListArray objectAtIndex:j];
                    [self dataList:dataListOnEachGroup removeThisList:previousList];
                }
                iDisplayName=[[MyUtilities getLocalizeStringFromTag:@"HistoryLast7Days"] stringByAppendingFormat:@" (%i)",dataListOnEachGroup.count];
                break;
                
            case Last30Days:
                dataListOnEachGroup=[[[StaticVariables shareVariables]historyAccess] getAllRecordsBetweenDate:[NSDate dateWithTimeIntervalSinceNow:-30*numberOfSecondsInOneDay]
                                                                    andDate:[NSDate date]];
                //exclude all objects belong to previous list
                for(int j=0;j<i;j++){
                    NSMutableArray* previousList=[groupDataListArray objectAtIndex:j];
                    [self dataList:dataListOnEachGroup removeThisList:previousList];
                }
                iDisplayName=[[MyUtilities getLocalizeStringFromTag:@"HistoryLast30Days"] stringByAppendingFormat:@" (%i)",dataListOnEachGroup.count];
                break;
        }
        [groupDataListArray addObject:dataListOnEachGroup];
        [groupDisplayNameArray replaceObjectAtIndex:i withObject:iDisplayName];
    }
}
-(NSMutableArray *)getDataListOf:(HistoryGroupName)_time{
    return [groupDataListArray objectAtIndex:_time];
}

//we assume that the _dataList contained all objects from _removeList
-(void)dataList:(NSMutableArray*)_dataList removeThisList:(NSMutableArray*)_removeList{
    for(int i=0;i<_removeList.count;i++){
        HistoryModel* iHistory=[_removeList objectAtIndex:i];
        for(int j=0;j<_dataList.count;j++){
            HistoryModel* jHistory=[_dataList objectAtIndex:j];
            if([iHistory.historyID isEqualToString:jHistory.historyID]){
                [_dataList removeObject:jHistory];
                break;
            }
        }
    }
}



@end
